package com.faf.ui;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.faf.dao.impl.AccessDaoImpl;
import com.faf.dao.impl.FoodDaoImpl;
import com.faf.dao.impl.LikesDaoImpl;
import com.faf.service.impl.ItemSimilarityImpl;
import com.faf.bean.Food;

/**
 * Servlet implementation class List
 */
@WebServlet("/List")
public class gList extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public gList() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		FoodDaoImpl foodDao = new FoodDaoImpl();
		ArrayList<Food> foodList = null;

		String strFlag = request.getParameter("flag");
		request.setAttribute("strFlag", strFlag);
		String strSearch = request.getParameter("serchContect");
		int iPage = null == request.getParameter("page") ? 1 : Integer.parseInt(request.getParameter("page"));
		int iCountPerPage = 10;
		int iCountFood = 0;
		int iCountPage = 0;

		if (null != strFlag) {

			int iType = Integer.parseInt(strFlag);

			try {
				switch (iType) {
				case 1: { // 炒菜
					foodList = foodDao.getFoodByType("炒菜", iPage, iCountPerPage);
					iCountFood = foodDao.getFoodCountByType("炒菜");
					break;
				}
				case 2: { // 主食
					foodList = foodDao.getFoodByType("主食", iPage, iCountPerPage);
					iCountFood = foodDao.getFoodCountByType("主食");
					break;
				}
				case 3: { // 凉菜
					foodList = foodDao.getFoodByType("凉菜", iPage, iCountPerPage);
					iCountFood = foodDao.getFoodCountByType("凉菜");
					break;
				}
				case 4: { // 饮品
					foodList = foodDao.getFoodByType("饮品", iPage, iCountPerPage);
					iCountFood = foodDao.getFoodCountByType("饮品");
					break;
				}
				case 5: { // 速食
					foodList = foodDao.getFoodByType("速食", iPage, iCountPerPage);
					iCountFood = foodDao.getFoodCountByType("速食");
					break;
				}
				case 6: { // 水果
					foodList = foodDao.getFoodByType("水果", iPage, iCountPerPage);
					iCountFood = foodDao.getFoodCountByType("水果");
					break;
				}
				default: { // 全部
					foodList = foodDao.getFoods();
					iCountFood = foodDao.getFoodCount();
					break;
				}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		else {
			System.out.println(strSearch);
			try {
				foodList = foodDao.getFoodsByLike(null == strSearch ? "" : strSearch, iPage, iCountPerPage);
				iCountFood = foodDao.getFoodCountByLike(strSearch);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		ItemSimilarityImpl itemsi = new ItemSimilarityImpl();
		ArrayList<Food> meFood = new ArrayList<Food>();

		if (null != request.getSession().getAttribute("account")) {
			try {
				System.out.println(request.getSession().getAttribute("id"));
				ArrayList<Integer> food2me = itemsi.recommend((int) request.getSession().getAttribute("id"));
				for (int id : food2me) {
					meFood.add(foodDao.getFoodByFoodID(id));
				}
				if(meFood.isEmpty()) {
					AccessDaoImpl aDao = new AccessDaoImpl();
					ArrayList<Integer> list = aDao.getFoodTopList();
					for (int i = 0; i < list.size(); i++) {
						meFood.add(foodDao.getFoodByFoodID(list.get(i)));
						if (i == 1)
							break;
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			AccessDaoImpl aDao = new AccessDaoImpl();
			try {
				ArrayList<Integer> list = aDao.getFoodTopList();
				for (int i = 0; i < list.size(); i++) {
					meFood.add(foodDao.getFoodByFoodID(list.get(i)));
					if (i == 1)
						break;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// for(int i = 0; i < )
		}

		request.setAttribute("mefood", meFood);

		iCountPage = iCountFood / iCountPerPage + (iCountFood % iCountPerPage == 0 ? 0 : 1);
		request.setAttribute("list_dat_food", foodList);
		request.setAttribute("page", iPage);
		request.setAttribute("page-count", iCountPage);
		request.setAttribute("flag", strFlag);
		request.setAttribute("serchContect", strSearch);

		request.getRequestDispatcher("/UserGlobal/list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
